<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>请求重复问题</title>
</head>
<body>
    <button>发送请求</button>
    <script>
        const btns = document.querySelectorAll('button');
        let xhr = null;
        let isSending = false;
        btns[0].onclick = function(){
            if(isSending) xhr.abort();//如果请求进行中，则取消请求，重新创建请求
            xhr = new XMLHttpRequest();
            isSending = true;//正在请求为true
            xhr.open('GET', 'http://localhost:8000/delay');
            xhr.send();
            xhr.onreadystatechange = function(){
                if(xhr.readyState === 4){
                    isSending = false;//请求完成变为false
                    if(xhr.status >= 200 && xhr.status < 300){
                        console.log(xhr.response)
                    }
                }
            }
        }
    </script>
</body>
</html>